/**
 * ActiveMQ Anonymous User - An authentication plug-in to provide 
 * access to ActiveMQ for a mix of authenticated and non-authenticated 
 * users.
 * 
 * Copyright (C) 2009 Global Radio
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.thisisglobal.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.activemq.jaas.GroupPrincipal;
import org.apache.activemq.security.AuthenticationUser;

public class SimpleAuthenticationPlugin implements BrokerPlugin {

	private Map userGroups;
	
	private Map userPasswords;
	
	private String anonymousUser;
	
	private String anonymousGroup;
	
	public SimpleAuthenticationPlugin() { }
	
	public SimpleAuthenticationPlugin(List users) {
		setUsers(users);
	}

	/**
	 * @see org.apache.activemq.broker.BrokerPlugin#installPlugin(org.apache.activemq.broker.Broker)
	 */
	public Broker installPlugin(Broker parent) throws Exception {
		SimpleAuthenticationBroker broker = new SimpleAuthenticationBroker(parent, userPasswords, userGroups);
		if(anonymousUser != null) {
			broker.setAnonymousUser(anonymousUser);
		}
		if(anonymousGroup != null) {
			broker.setAnonymousGroup(anonymousGroup);
		}
		return broker;
	}
	
	public void setUserGroups(Map users) {
		this.userGroups = users;
	}
	
	public Map getUserGroups() {
		return userGroups;
	}
	
	public void setUserPasswords(Map users) {
		this.userPasswords = users;
	}
	
	public Map getUserPasswords() {
		return userPasswords;
	}
	
	/**
	 * Sets individual users for authentication
	 * 
	 * @org.apache.xbean.ElementType class="org.apache.activemq.security.AuthenticationUser"
	 */
	public void setUsers(List users) {
		this.userPasswords = new HashMap();
	    this.userGroups = new HashMap();
	    for (Iterator it = users.iterator(); it.hasNext();) {
	    	AuthenticationUser user = (AuthenticationUser)it.next();
	    	userPasswords.put(user.getUsername(), user.getPassword());
	    	Set groups = new HashSet();
	    	StringTokenizer iter = new StringTokenizer(user.getGroups(), ",");
	    	while (iter.hasMoreTokens()) {
	    		String name = iter.nextToken().trim();
	    		groups.add(new GroupPrincipal(name));
	    	}
	    	userGroups.put(user.getUsername(), groups);
	    }
	}   
	
	public void setAnonymousUser(String anonymousUser) {
		this.anonymousUser = anonymousUser;
	}
	
	public void setAnonymousGroup(String anonymousGroup) {
		this.anonymousGroup = anonymousGroup;
	}
	
	

}
